// pages/couponList/index.js
const {
  post,
  base
} = getApp();
Page({
  data: {
    active: 0,
    show: false,
    showData: {
      ticket_desc: "",
      ticket_amount: 0,
      ticket_name: "",
      imgUrl: '',
    },
    data: [{
        params: {
          status: 2,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        dataList: []
      },
      {
        params: {
          status: 3,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        dataList: []
      },
      {
        params: {
          status: 5,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        dataList: []
      }
    ]
  },
  // 初始化内容
  onLoad: function () {
    this.getList();
  },
  // 获取数据
  getList() {
    if (this.data.data[this.data.active].showLoading) {
      post('/coupon/list', this.data.data[this.data.active].params).then(res => {
        if (res.result == 1) {
          this.setData({
            [`data[${this.data.active}].dataList`]: this.data.data[this.data.active].dataList.concat(res.data)
          });
          if (res.data.length < this.data.data[this.data.active].params.pageSize) {
            this.setData({
              [`data[${this.data.active}].showLoading`]: false
            })
          }
        }
      })
    }
  },
  // 改变状态
  changeActive(e) {
    this.setData({
      active: e.detail.index
    });
    if (this.data.data[this.data.active].dataList.length == 0) {
      this.getList();
    }
  },
  // 上拉加载
  bindscrolltolower: function () {
    this.setData({
      [`data[${this.data.active}].page`]: this.data.data[this.data.active].page += 1
    });
    this.getList();
  },
  // 去使用
  goToUser(e) {
    if (e.currentTarget.dataset.origin_type == 1) {
      post('/coupon/detail', {
        ticket_num: e.currentTarget.dataset.ticket_num
      }).then(res => {
        if (res.result == 1) {
          this.setData({
            show: true,
            showData: {
              ticket_desc: res.data.ticket_desc,
              ticket_amount: res.data.ticket_amount,
              ticket_name: res.data.ticket_name,
              imgUrl: `${base}qrcode/create?text=${res.data.out_trade_no_dyna}&w=230&h=230`
            }
          })
        };
      });
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '进入商城首页使用？',
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/home/index'
            });
          }
        }
      });
    }
  },
  // 关闭
  closeShow() {
    this.setData({
      show: false
    })
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})